package Controller.Employee;

import Dto.Employee;
import Service.EmployeeService;
import Service.UserService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet(name = "updateEmployee", value = "/UpdateEmployee")
public class UpdateEmployee extends HttpServlet {
    private EmployeeService employeeService = new EmployeeService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //转码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        //获取参数
        Integer id = Integer.valueOf(request.getParameter("id"));
        String name = request.getParameter("name");
        String idcard = request.getParameter("idcard");
        String gender = request.getParameter("gender");
        String phone = request.getParameter("phone");
        String address = request.getParameter("address");
        String hiredate = request.getParameter("hiredate");
        String status = request.getParameter("status");
        //创建employee对象
        Employee employee=new Employee();
        //存入参数
        employee.setId(id);
        employee.setName(name);
        employee.setIdcard(idcard);
        employee.setGender(gender);
        employee.setPhone(phone);
        employee.setAddress(address);
        employee.setHiredate(hiredate);
        employee.setStatus(status);
        //调用service层方法
        try {
            employeeService.updateEmployee(employee);
            //如果成功,请求转发到listEmployee.jsp
            request.getRequestDispatcher("/ListEmployee").forward(request, response);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
